<template>
	<view class="fui-wrap">
		<fui-result v-if="err" type="fail" :title="errTitle" :descr="err">
			<fui-button @click="$reLaunch('merchant/index')" width="400rpx" height="84rpx" text="返回主页" type="success" bold :margin="['48rpx','0','24rpx']"></fui-button>
		</fui-result>
		<view class="qrcode-box" v-else>
			<fui-result v-if="show" type="success" title="扫码成功" :descr="`用户【${scaninfo[scaninfo.length-1].third.openname}】扫码成功`">
				<fui-button @click="changeQrcode" width="400rpx" height="84rpx" text="继续扫码" type="success" bold :margin="['48rpx','0','24rpx']"></fui-button>
				<fui-button @click="$reLaunch('merchant/index')" width="400rpx" height="84rpx" text="返回主页" type="primary" bold></fui-button>
			</fui-result>
			<block v-else>
				<fui-text :text="coupon.title" type="black" size="32" block align="center" fontWeight="800"></fui-text>
				<image :src="url" mode="widthFix"></image>
				<view class="expire">
					<fui-count-down v-if="expiretime" :isDays="true" :isColon="false" borderColor="transparent" background="transparent" :value="expiretime" @end="endQrcode"></fui-count-down>
					<fui-text text="过期" type="danger" size="28" fontWeight="800" style="margin-left: 10rpx;"></fui-text>
				</view>
				<view class="gocars" @click="goPushPlate">
					<fui-text text="代领券，去填写车牌信息" decoration="underline" type="primary" size="28" block fontWeight="800"></fui-text>
					<fui-icon name="pulldown-fill" color="#465CFF" style="transform: rotate(270deg);"></fui-icon>
				</view>
				<view class="buttom">
					<fui-button height="84rpx" @click="download" text="下载" type="primary" bold :margin="['60rpx','0','40rpx']"></fui-button>
					<fui-button height="84rpx" @click="expire" text="手动过期" type="danger" bold></fui-button>
				</view>	
			</block>
		</view>
		<pcfooter></pcfooter>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	import pcfooter from '@/components/common/pcfooter.vue';
	export default {
		components:{
			pcfooter:pcfooter
		},
		data() {
			return {
				err:'',
				errTitle:'操作失败',
				coupon_id:'',
				coupon:'',
				url:'',
				qrcode:'',
				expiretime:'',
				initscan:false,
				show:false,
				scaninfo:[],
				hidepage:false,
				platform:''
			}
		},
		onLoad(e) {
			this.coupon_id=e.id;
			this.$platform();
			this.getCouponQrcode();
		},
		onUnload() {
			this.hidepage=true;
		},
		methods: {
			...methods,
			getCouponQrcode:function(id){
				this.$get('merchant/index/qrcode',{type:'static',coupon_id:this.coupon_id},true).then(res=>{
					this.url=res.url;
					this.coupon=res.coupon;
					this.qrcode=res.qrcode;
					this.expiretime=res.qrcode.expiretime-parseInt((new Date()).getTime()/1000);
					this.checkScan();
				}).catch(err=>{
					this.err=err.msg;
				});
			},
			checkScan:function(){
				if(this.hidepage){
					return;
				}
				if(this.show){
					setTimeout(()=>{
						this.checkScan();
					},2000);
					return;
				}
				this.$get('merchant/index/check-scan/static',{qrcode_id:this.qrcode.id}).then(res=>{
					if(!this.initscan){
						this.initscan=true;
					}else if(res.length>0 && this.scaninfo.length==0){
						this.show=true;
					}else if(res.length>0 && this.scaninfo.length>0 && res[res.length-1].scantime!=this.scaninfo[this.scaninfo.length-1].scantime){
						this.show=true;
					}
					this.scaninfo=res;
					setTimeout(()=>{
						this.checkScan();
					},2000);
				});
			},
			changeQrcode:function(){
				this.show=false;
			},
			endQrcode:function(){
				this.err='小程序码已过期，请重新获取';
			},
			goPushPlate:function(){
				this.$redirectTo('merchant/plate?qrcode_id='+this.qrcode.id);
			},
			download:function(){
				if(this.platform=='PC'){
					location.href=baseUrl+"merchant/index/download-img?qrcode_id="+this.qrcode.id;
				}else{
					let that=this;
					uni.downloadFile({
						url: that.url,
						success: (res) => {
							if (res.statusCode === 200) {
								uni.saveImageToPhotosAlbum({
									filePath: res.tempFilePath,
									success: function () {
										that.$msg('下载成功！')
									}
								});
							}
						}
					});
				}
			},
			expire:function(){
				let that=this;
				uni.showModal({
					content:'你确定要将当前二维码手动过期吗？',
					success(e) {
						if(e.confirm){
							that.$post('merchant/index/expire',{
								qrcode_id:that.qrcode.id,
								coupon_id:that.coupon_id,
							},true).then(res=>{
								that.errTitle='';
								that.err='小程序码已过期，请重新获取!';
							});
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fui-wrap{
		background-color: #fff;
	}
	.qrcode-box{
		padding: 40rpx 0 20rpx 0;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		image{
			width: 400rpx;
			height: 400rpx;
			margin-top: 40rpx;
		}
		.expire{
			margin-top: 40rpx;
		}
		.gocars{
			display: flex;
			justify-content: center;
			align-items: center;
			width: 80%;
			margin: 40rpx auto;
		}
		.buttom{
			width: 92%;
			margin: 0 auto;
			padding-bottom: 30rpx;
		}
	}
</style>